package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.util.JSONResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("department")
public class DepartmentController {
    @Autowired
    IDepartmentService departmentService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"department:view","部门页面"},logical= Logical.OR)
    public String view() {
        return "department";
    }

    @RequestMapping("getAll")
    @ResponseBody
    public List<Department> getAll() {
        return departmentService.getAll();
    }

    @RequestMapping("validateUsername")
    @ResponseBody
    public boolean validateUsername(String username, Long id) {
        Department department = departmentService.getByUsername(username);
        return department == null || department.getId() == id;
    }
    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"department:save","保存部门"},logical= Logical.OR)
    @ResponseBody
    public JSONResult saveOrUpdate(Department department){
        try {
            departmentService.saveOrUpdate(department);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false,"列表更新失败");
        }
        return new JSONResult(true,"恭喜您执行成功");
    }
    @RequestMapping("changeState")
    @RequiresPermissions(value = {"department:change","修改部门状态"},logical= Logical.OR)
    @ResponseBody
    public JSONResult changeState(Long id){
        try {
            departmentService.changeState(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false,"状态更新失败");
        }
        return new JSONResult(true,"状态更新成功");
    }
}
